Thursday, May 10, 2007

RectangleTest2.java

RectangleTest2.java

public class RectangleTest2 {
public static void main(String[] args) {
Rectangle2 r;

r = new Rectangle2(10,20,30,40,"white");

System.out.println(r);
r.setLocation(60,70).setSize(80,90).setColor("blue");
System.out.println(r);
}
}

class Rectangle2 {
private int x, y;
private int width, height;
private String color;

public Rectangle2(int x, int y, int w, int h, String c) {
if(x >= 0 && y >= 0 &&
w > 0 && h > 0 &&
c != null)
{
this.x = x;
this.y = y;
this.width = w;
this.height = h;
this.color = c;
}
else {
x = y = 0;
width = height = 1;
color = "black";
}
}

public Rectangle2 setLocation(int x, int y) {
this.x = x;
this.y = y;
return(this);
}

public Rectangle2 setSize(int width, int height) {
this.width = width;
this.height = height;
return(this);
}

public Rectangle2 setColor(String color) {
this.color = color;
return(this);
}

public String toString() {
String str;

str = "x : " + x + "\n" +
"y : " + y + "\n" +
"width : " + width + "\n" +
"height : " + height + "\n" +
"color : " + color + "\n";
return(str);
}
}

RectangleTest2.java output

x : 10
y : 20
width : 30
height : 40
color : white
x : 60
y : 70
width : 80
height : 90
color : blue


Tag: Study Code Program Java

No comments:

Post a Comment